package com.study.concurrency;

import java.util.concurrent.locks.ReentrantLock;

//要求一个人下班之后，另一个人才能上班，即不能两个人同时上班
public class ReentrantLockDemo {
	public static ReentrantLock reentrantLock = new ReentrantLock();
	public static void main(String[] args) {
		for(int i = 0 ; i < 10; i++){
			new Thread(new Worker1()).start();
		}
	}
}
class Worker1 implements Runnable{
	
	public void workOn(){
		System.out.println(Thread.currentThread().getName() + "上班.");
	}
	public void workOff(){
		System.out.println(Thread.currentThread().getName() + "下班.");
	}
	@Override
	public void run() {
		ReentrantLockDemo.reentrantLock.lock();
		//ReentrantLockDemo.reentrantLock.lock();
		this.workOn();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.workOff();
		ReentrantLockDemo.reentrantLock.unlock();
//		ReentrantLockDemo.reentrantLock.unlock();  //lock释放两次会报错，Exception in thread "Thread-6" java.lang.IllegalMonitorStateException
	} 
	
	
}